package com.tgg.treeproject.controller;


import cn.hutool.core.bean.BeanUtil;
import com.tgg.treeproject.pojo.InforationData;
import com.tgg.treeproject.service.InforationDataService;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 田国刚
 * @since 2023-04-21
 */
@RestController
@RequestMapping("/inforation-data")
public class InforationDataController {
    @Resource
    InforationDataService inforationDataService;

    @PostMapping("/get")
    @ResponseBody
    public ResponseEntity<Map<String, Object>> getDataById(@RequestBody Map<String, Object> resout) {
        String id = resout.get("id").toString();
        Integer current = (Integer) resout.get("current");
        Integer size = (Integer) resout.get("size");
        HashMap<String, Object> inforationData = inforationDataService.getInforationData(id, current, size);
        return new ResponseEntity<>(inforationData, HttpStatus.OK);
    }

    @PostMapping("/save")
    @ResponseBody
    public ResponseEntity<Map<String, Object>> saveData(@RequestBody Map<String, Object> resout) {
        InforationData bean = BeanUtil.toBean(resout, InforationData.class);
        HashMap<String, Object> map;
        if (StringUtils.isEmpty(bean.getId())) {
            map = inforationDataService.saveInforationData(bean);
        } else {
            map = inforationDataService.updateInforationData(bean);
        }
        return new ResponseEntity<>(map, HttpStatus.OK);
    }
    @PostMapping("/delete")
    @ResponseBody
    public ResponseEntity<Map<String, Object>> deleteData(@RequestBody Map<String, Object> resout) {
        String id = resout.get("id").toString();
        HashMap<String, Object> map;

            map = inforationDataService.deleteInforationData(id);

        return new ResponseEntity<>(map, HttpStatus.OK);
    }

}
